package cn.qiyu5522.controller;


import cn.qiyu5522.domain.DTO.ArticleAddDTO;
import cn.qiyu5522.domain.DTO.ArticlePageQueryDTO;
import cn.qiyu5522.domain.DTO.ArticleUpdateDTO;
import cn.qiyu5522.domain.entity.ResponseResult;
import cn.qiyu5522.service.ArticleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author Tonkey
 * @since 2024-04-24
 */
@RestController
@RequestMapping("/content/article")
public class ArticleController {

    @Resource
    private ArticleService articleService;
    @PostMapping()
    public ResponseResult addArticle(@RequestBody ArticleAddDTO articleAddDTO){
        return articleService.addArticle(articleAddDTO);
    }


    @PreAuthorize("@ps.hasPermission('content:article:list')")
    @GetMapping("/list")
    public ResponseResult list(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam( required = false) String title,
                               // 未来可能加入的查询条件
                               @RequestParam( required = false) String summary){
        ArticlePageQueryDTO articlePageQueryDTO = new ArticlePageQueryDTO(pageNum, pageSize, title, summary);
        return articleService.getArticleListByCondition(articlePageQueryDTO);
    }

    @GetMapping("/{id}")
    public ResponseResult getArticleById(@PathVariable Long id){
        return articleService.getArticleById(id);
    }

    @PutMapping
    public ResponseResult updateArticle(@RequestBody ArticleUpdateDTO  articleUpdateDTO){
        return articleService.updateArticle(articleUpdateDTO);
    }

    @DeleteMapping("/{id}")
    public ResponseResult deleteArticle(@PathVariable Long id){
        return articleService.deleteArticle(id);
    }
}
